using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Controls_DoctorSelect : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private string mDoctorIdHiddenField;

    public string DoctorIdHiddenField
    {
        get
        {
            Control ctrl = Parent.FindControl(mDoctorIdHiddenField);
            if (ctrl != null)
            {
                return ctrl.ClientID;
            }
            return string.Empty;
        }
        set
        {
            mDoctorIdHiddenField = value;
        }
    }

    private string mDoctorTextBox;

    public string DoctorTextBox
    {
        get
        {
            Control ctrl = Parent.FindControl(mDoctorTextBox);
            if (ctrl != null)
            {
                return ctrl.ClientID;
            }
            return string.Empty;
        }
        set
        {
            mDoctorTextBox = value;
        }
    }
    
    public string GetFunctionCallString(object doctorId, object firstName, object middleName, object lastName)
    {
        string functionCallString = string.Format("UpdateDoctorFields('{0}', '{1}', '{2}', '{3}')", doctorId,
            firstName, middleName, lastName);
        return functionCallString;
    }
    
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string firstName = tbName.Text;
        string middleName = tbSurname.Text;
        string familyName = tbFamilyName.Text;

        StringBuilder filterExpression = new StringBuilder();

        bool filterIsEmpty = true;
        if (firstName != string.Empty)
        {
            filterExpression.Append(string.Format("FirstName=" + "'{0}'", firstName));
            filterIsEmpty = false;
        }

        if (middleName != string.Empty)
        {
            if (!filterIsEmpty)
            {
                filterExpression.Append(" AND ");
            }
            filterExpression.Append(string.Format("MiddleName=" + "'{0}'", middleName));
            filterIsEmpty = false;
        }

        if (familyName != string.Empty)
        {
            if (!filterIsEmpty)
            {
                filterExpression.Append(" AND ");
            }
            filterExpression.Append(string.Format("LastName=" + "'{0}'", familyName));
        }
        
        if (filterExpression.Length > 0)
        {
            odsDoctors.FilterExpression = filterExpression.ToString();
        }
        gvDoctors.SelectedIndex = -1;
        gvDoctors.DataBind();
    }
    protected void gvDoctors_Sorting(object sender, GridViewSortEventArgs e)
    {
        gvDoctors.SelectedIndex = -1;
    }
}
